package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.TestAnswersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.TestAnswersForm;
import com.danciyixue.boot.dcyx.model.query.TestAnswersQuery;
import com.danciyixue.boot.dcyx.model.vo.TestAnswersVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 测试答案选项前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:29
 */
@Tag(name = "测试答案选项接口")
@RestController
@RequestMapping("/api/v1/test-answers")
@RequiredArgsConstructor
public class TestAnswersController  {

    private final TestAnswersService testAnswersService;

    @Operation(summary = "测试答案选项分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:test-answers:query')")
    public PageResult<TestAnswersVO> getTestAnswersPage(TestAnswersQuery queryParams ) {
        IPage<TestAnswersVO> result = testAnswersService.getTestAnswersPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增测试答案选项")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:test-answers:add')")
    public Result<Void> saveTestAnswers(@RequestBody @Valid TestAnswersForm formData ) {
        boolean result = testAnswersService.saveTestAnswers(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取测试答案选项表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:test-answers:edit')")
    public Result<TestAnswersForm> getTestAnswersForm(
        @Parameter(description = "测试答案选项ID") @PathVariable Long id
    ) {
        TestAnswersForm formData = testAnswersService.getTestAnswersFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改测试答案选项")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:test-answers:edit')")
    public Result<Void> updateTestAnswers(
            @Parameter(description = "测试答案选项ID") @PathVariable Long id,
            @RequestBody @Validated TestAnswersForm formData
    ) {
        boolean result = testAnswersService.updateTestAnswers(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除测试答案选项")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:test-answers:delete')")
    public Result<Void> deleteTestAnswerss(
        @Parameter(description = "测试答案选项ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = testAnswersService.deleteTestAnswerss(ids);
        return Result.judge(result);
    }
}
